#!/bin/bash

CURDIR=`pwd`
SHINKEN_PACKAGE=shinken-1.4.tar.gz
SHINKEN_URI=http://www.shinken-monitoring.org/pub/$SHINKEN_PACKAGE

download_url() {
        if [ -x "/usr/bin/curl" ] ; then
            /usr/bin/curl -# -O $1
        elif [ -x "/usr/bin/wget" ] ; then
            /usr/bin/wget -q $1
        else
            echo "Unable to install Shinken: can't find wget or curl in /usr/bin."
            exit 1
        fi
    }

do_with_root() {
        # already root? "Just do it" (tm).
        if [ `whoami` = 'root' ] ; then
            $*
        elif [ -x /bin/sudo -o -x /usr/bin/sudo ] ; then
            echo
            echo "Sudo detected, Shinken will request root privileges to"
            echo "install. You may be prompted for a password. If you prefer to not use"
            echo "sudo, please re-run this script as root."
            echo "sudo $*"
            sudo $*
        else
            echo "Shinken requires root privileges to install. Please re-run this script as"
            echo "root."
            exit 1
        fi
    }


TMPDIR=`mktemp -d -t shinken-install-XXXXXXX`
echo
echo "Preparing the temporary directory " $TMPDIR
cd "$TMPDIR"

download_url $SHINKEN_URI
echo
echo "Unziping the Shinken package" $SHINKEN_PACKAGE
tar xfz $SHINKEN_PACKAGE
cd shinken-*


do_with_root ./install -i
do_with_root ./install -p nagios-plugins
do_with_root ./install -p check_mem
do_with_root ./install -p manubulon
do_with_root ./install -a pnp4nagios
do_with_root ./install -p check_netint


# Bail out
echo "Exiting"
cd "$CURDIR"
rm -rf "$TMPDIR"
